package com.qingke.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.qingke.mapper.EmployeeAccountMapper;
import com.qingke.mapper.EmployeeJurisdictionMapper;
import com.qingke.mapper.EmployeeMapper;
import com.qingke.mapper.JurisdictionMapper;
import com.qingke.pojo.Employee;
import com.qingke.pojo.EmployeeAccount;
import com.qingke.pojo.EmployeeJurisdiction;
import com.qingke.service.EmployeeService;

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
	private static Logger logger = Logger.getLogger(EmployeeServiceImpl.class);
	@Resource
	private EmployeeMapper emDao;
	@Resource
	private EmployeeAccountMapper eaDao;
	@Resource
	private EmployeeJurisdictionMapper ejdDao;
	@Resource
	private JurisdictionMapper jdDao;

	public Employee getEmployeeById(int employeeId) {
		return emDao.selectByPrimaryKey(employeeId);
	}

	public Employee getEmployeeeByEmployeeNumber(String employeenumber) {
		return emDao.selectByEmployeeNumber(employeenumber);
	}

	public boolean canEmployeeLogin(String employeeNumber, String password,String name) {
		int employeeId=emDao.selectByEmployeeNumber(employeeNumber).getId();
		List<EmployeeAccount> employeeAccount=eaDao.selectByPassword(password);
		for(EmployeeAccount ea:employeeAccount){
			if(employeeId==ea.getId()){
				List<EmployeeJurisdiction> list=ejdDao.selectByEmployeeId(employeeId);
				for(EmployeeJurisdiction ejd:list){
					if(name.equals(jdDao.selectByPrimaryKey(ejd.getJurisdictionId()).getName())){
						return true;
					}
				}
			}
		}
		return false;
	}

	public boolean isEmployeeExisted(String employeeNumber) {
		return emDao.selectByEmployeeNumber(employeeNumber)!=null;
	}

	public int softDeleteEmployee(int employeeId) {
          return emDao.softDelete(employeeId);
	}

	public int updateEmployeeByPrimaryKey(Employee record) {
		return  emDao.updateByPrimaryKey(record);
	}
	
	public int saveEmployee(Employee employee) {
		logger.debug("new employee id ="+ employee.getId());
		return  emDao.insert(employee);
              
	}

	public int saveEmployeeAccount(EmployeeAccount employeeAccount) {
	logger.debug("new employeeAccount id ="+ employeeAccount.getId());
       return eaDao.insert(employeeAccount);
	}

	public int updateEmployeeAccountByPrimaryKey(EmployeeAccount record) {
		return eaDao.updateByPrimaryKey(record);
	}

	public EmployeeAccount getEmployeeAccountById(int employeeAccountId) {
		return eaDao.selectByPrimaryKey(employeeAccountId);
	}

	public int saveEmployeeJurisdiction(EmployeeJurisdiction record) {
         return ejdDao.insert(record);
	}

	public int updateEmployeeJurisdictionByPrimaryKey(EmployeeJurisdiction record) {
		return ejdDao.updateByPrimaryKey(record);
	}

	public EmployeeJurisdiction getEmployeeJurisdictionById(int employeeJurisdictionId) {
		return ejdDao.selectByPrimaryKey(employeeJurisdictionId);
	}

	public List<EmployeeAccount> getEmployeeAccountByPassword(String password) {
		return eaDao.selectByPassword(password);
	}

	public List<Employee> listEmployee() {
		return emDao.listEmployee();
	}

	public List<EmployeeJurisdiction> listEmployeeJurisdictionByEmployeeId(int employeeId) {
		return ejdDao.selectByEmployeeId(employeeId);
	}

	public EmployeeJurisdiction getEmployeeJurisdictionByEmployeeId(int employeeId) {
		return ejdDao.getByEmployeeId(employeeId);
	}

	public Employee getEmployeeByName(String name) {
		return emDao.selectByName(name);
	}



}
